package com.glink.gateway.service.authorize.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.glink.common.contants.BaseCodeConstants;
import com.glink.common.util.BaseCommonUtils;
import com.glink.gateway.common.LogRecordUtils;
import com.glink.gateway.mapper.AuthMapper;
import com.glink.gateway.service.authorize.AuthService;
import com.glink.gateway.vo.UserVO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Objects;

/**
 * Created by IntelliJ IDEA.
 *
 * @Author : qiushaoshan
 * @create 2024/6/24 16:09
 */
@Service
@Slf4j
public class AuthServiceImpl implements AuthService {
    
    @Autowired
    private AuthMapper authMapper;
    
    @Override
    public boolean checkAuthUrlByUserRole(UserVO userVO, String requestUrl, String remoteIp){
        
        if(StringUtils.isBlank(requestUrl)){
            return true;
        }

        int index = requestUrl.indexOf("/", 6);
        if(index >= 0){
            requestUrl = requestUrl.substring(index);
        }

        // 查看地址是否存在功能点配置
        List<String> containUriMenuIdList = authMapper.findMenuIdByMenuIdAndUri(null, requestUrl);
        // 地址有菜单的功能点记录，因此需要辨别权限
        if(CollectionUtil.isNotEmpty(containUriMenuIdList)){
            // 查询用户所属的角色
            List<String> roleIdList = authMapper.findRoleIdByUserId(userVO.getId());
            if(CollectionUtil.isNotEmpty(roleIdList)){
                // 查询角色所属菜单
                List<String> menuIdList = authMapper.findMenuIdByRoleIdIn(roleIdList);
                // INSTR(column_name, 'substring') > 0;
                if(CollectionUtil.isNotEmpty(menuIdList)){
                    // 查询权限范围内的包含地址的菜单
                    List<String> authContainUriMenuIdList = authMapper.findMenuIdByMenuIdAndUri(menuIdList, requestUrl);
                    if(CollectionUtil.isEmpty(authContainUriMenuIdList)){
                        // 没有授权     
                        log.error("9. 地址没有得到授权，不允许放行");
                        String logMsg = LogRecordUtils.interfaceAuth(userVO.getId(), requestUrl);

                        authMapper.saveSysLog(BaseCommonUtils.generateUUID(), BaseCodeConstants.CodeType_LogType.KEY_LogType02, userVO.getId(), userVO.getFullName(), logMsg, requestUrl, remoteIp);
                        return false;
                    }else{
                        log.error("10. 地址得到了授权，放行");
                        return true;
                    }
                }
            }
        }else{
            log.info("8.地址没有限权，直接放行");
            return true;
        }
        return true;
    }

    /**
     * 校验用户角色信息是否存在
     *
     * @param userVO 用户信息
     * @return 状态
     */
    @Override
    public boolean checkRole(UserVO userVO) {
        List<String> roleIdList = authMapper.findRoleIdByUserId(userVO.getId());
        if(CollectionUtil.isNotEmpty(roleIdList)){
            return true;
        }
        return false;
    }

    /**
     * 校验用户单位信息
     *
     * @param userVO 用户单位
     * @return 状态
     */
    @Override
    public boolean checkOrg(UserVO userVO) {
        if(StringUtils.isBlank(userVO.getOrgId())){
            return false;
        }
        Integer num = authMapper.countOrgByOrgId(userVO.getOrgId());
        return Objects.nonNull(num) && num > 0;
    }
}
